
package sprites;

import org.newdawn.slick.Animation;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;

/**
 *
 * @author ant
 * 
 * Stores an animation for each direction of movement. This represents the
 * single instance of an animation associated with a single entity.
 */
public class EntityAnimationInstance
{
    public Animation [] animationsPerDir;
    private int crntAnimation;
    
    public EntityAnimationInstance(Animation [] animations, SpriteSheet sheet)
    {
        animationsPerDir = animations;
        crntAnimation = 0;
    }

    public void setDir(int dir)
    {
        crntAnimation = dir;
        startAnimation();
    }
    
    public void stopAnmation()
    {
        animationsPerDir[crntAnimation].setAutoUpdate(false);
    }
    
    public void startAnimation()
    {
        animationsPerDir[crntAnimation].restart();
        animationsPerDir[crntAnimation].setAutoUpdate(true);
    }
    
    public void render(Rectangle euclideanToRender, Vector2f posToRender)
    {
        //sheet.startUse();
        //can check if the animation exists in the rectangle before rendering
        animationsPerDir[crntAnimation].draw(posToRender.x - euclideanToRender.getX()-16,
                                             posToRender.y - euclideanToRender.getY()-16);
        //sheet.endUse();
    }
    
    //get the discrete animation direction to face based on the direction given
    public static int getDir(double angle)
    {
        if(angle < 0 || angle > Math.PI*2)
            throw new IllegalArgumentException("angle is outside of range 0 to 2 pi");
        
        if(angle < Math.PI/4 || angle > Math.PI*7/4)
            return 0;
        else if(angle < Math.PI*3/4)
            return 3;
        else if(angle < Math.PI*5/4)
            return 2;
        else
            return 1;
    }
    
}
